package com.chenfan.filestore.model;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.filestore.dto.BatchExportTemplateDTO;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.infra.vo.UserVO;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.Objects;

@Data
@TableName(value = "batch_export_template")
public class BatchExportTemplate extends BaseEntity {

    @TableId
    private Long templateId;
    /**
     * 模板Code
     */
    private String templateCode;
    /**
     * 模板名称
     */
    private String templateName;
    /**
     * 所属系统id
     */
    private Long systemId;
    /**
     * 所属系统名称
     */
    private String systemName;
    /**
     * 所属模块id
     */
    private Long moduleId;
    /**
     * 模块名称
     */
    private String moduleName;
    /**
     * 模板url
     */
    private String templateUrl;
    /**
     * 模板url名称
     */
    private String templateUrlName;
    /**
     * 数据库id
     */
    private Long databaseId;
    /**
     * 数据名称
     */
    private String databaseName;
    /**
     * sql 语句
     */
    private String sqlSource;

    /**
     * 批量大小
     */
    private Integer batchSize;
    /**
     * 备注
     */
    private String remark;

    private Integer status;

    private Integer isDelete;

    private Long tenantId;

    private Long companyId;

    private Long createBy;

    private String createName;

    private Date createDate;

    private Long updateBy;


    public static BatchExportTemplate ofDefault(BatchExportTemplateDTO batchExportTemplateDTO) {
        BatchExportTemplate batchExportTemplate = new BatchExportTemplate();
        BeanUtils.copyProperties(batchExportTemplateDTO, batchExportTemplate);

        batchExportTemplate.setUp();
        return batchExportTemplate;
    }

    public void setUp() {
        UserVO userVo = UserContextHolder.getUserVo();
        if (Objects.nonNull(userVo)) {
            setCreateBy(userVo.getUserId());
            setCreateName(userVo.getRealName());
            setTenantId(userVo.getTenantId());
            setCompanyId(userVo.getCompanyId());
        }
        setCreateDate(new Date());
        updateSelf();
    }

}